package cn.mothin.service.common;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.mothin.dao.OperationLinkDao;
import cn.mothin.frame.bean.ActionResult;
import cn.mothin.frame.core.base.BaseService;
import cn.mothin.frame.core.bean.OperationLinkData;
import cn.mothin.frame.core.interfaces.IPermissionRobot;
import cn.mothin.frame.util.StringUtil;
import cn.mothin.vo.common.UserHelperForm;

@Service("54067bef7b0bf81ac00a981f")
public class UserHelperService extends BaseService {
	@Autowired
	private IPermissionRobot robot;
	@Autowired
	private OperationLinkDao operationLinkDao;

	private void addSub(OperationLinkData operationLinkData,String action,boolean rowOperation,int ordinal) {
		List<Map<String,Object>> list=operationLinkDao.queryOperations(action,rowOperation,ordinal);
		int step=-1;
		for(int i=0;i<list.size();i++) {
			String url=list.get(i).get("action").toString();
			if(robot.checkPermission(url)!=0){
				continue;
			}
			int subOrdinal=Integer.valueOf(list.get(i).get("subOrdinal").toString());
			if(step==-1){
				step=subOrdinal-i;
			}else{
				if((subOrdinal-i)>step){
					step=subOrdinal-i;
					operationLinkData.addChild(new OperationLinkData(true));
				}
			}
			
			OperationLinkData sub = new OperationLinkData();
			sub.setId(list.get(i).get("id").toString());
			sub.setIco(StringUtil.isNullOrEmpty(list.get(i).get("ico").toString())?"fa fa-circle-o":list.get(i).get("ico").toString());
			sub.setName(list.get(i).get("name").toString());
			sub.setAction(url);
			operationLinkData.addChild(sub);
		}
	}
	
	private void queryOperations(String action,boolean rowOperation,List<OperationLinkData> tools){
		int step=-1;
		
		List<Map<String,Object>> list=operationLinkDao.queryOperations(action,rowOperation);
		for(int i=0;i<list.size();i++) {
			String url=list.get(i).get("action").toString();
			if(robot.checkPermission(url)!=0){
				continue;
			}
			int ordinal=Integer.valueOf(list.get(i).get("ordinal").toString());
			if(step==-1){
				step=ordinal-i;
			}else{
				if((ordinal-i)>step){
					step=ordinal-i;
					tools.add(new OperationLinkData(true));
				}
			}
			
			OperationLinkData operationLinkData=new OperationLinkData();
			operationLinkData.setAction(url);
			operationLinkData.setIco(StringUtil.isNullOrEmpty(list.get(i).get("ico").toString())?"fa fa-circle-o":list.get(i).get("ico").toString());
			operationLinkData.setId(list.get(i).get("id").toString());
			operationLinkData.setName(list.get(i).get("name").toString());
			addSub(operationLinkData,action,rowOperation,ordinal);
			tools.add(operationLinkData);
		}
	}
	
	/**
	 * 获取关联操作列表-JSON
	 * 
	 * @param form
	 * @return
	 */
	public String queryOperation(UserHelperForm userHelperForm,String basePath) {
		ActionResult result=ActionResult.getInstance();
		String action = userHelperForm.getUrl().replace(basePath, "");
		
		if (StringUtil.isNullOrEmpty(action)) {
			result.setOk(false);
		}

		if (action.contains("?")) {
			action = action.split("\\?")[0];
		}
		
		Map<String,List<OperationLinkData>> value=new HashMap<String,List<OperationLinkData>>();
		List<OperationLinkData> tools = new ArrayList<OperationLinkData>();
		List<OperationLinkData> rows = new ArrayList<OperationLinkData>();
		
		queryOperations(action,false,tools);
		queryOperations(action,true,rows);
		value.put("tools", tools);
		value.put("rows", rows);
		
		result.setValue(value);
		return result.toJson();
	}

	public String checkPermission(UserHelperForm userHelperForm){
		ActionResult result=ActionResult.getInstance();
		String action = userHelperForm.getUrl();
		
		if (StringUtil.isNullOrEmpty(action)) {
			result.setOk(false);
		}

		if(robot.checkPermission(action)==0){
			result.setOk(true);
		}else{
			result.setOk(false);
		}
		return result.toJson();
	}
}
